#ifndef SCOPEDSTREAMBUFASSIGN_H_INCLUDED__
#define SCOPEDSTREAMBUFASSIGN_H_INCLUDED__

#include <streambuf>

template <class charT, class traits = std::char_traits<charT> >
struct scoped_basic_streambuf_assignment
{
    typedef std::basic_ios<charT, traits> stream_type;
    typedef std::basic_streambuf<charT, traits> streambuf_type;

    stream_type& m_s;
    streambuf_type* m_orig_sb;

    scoped_basic_streambuf_assignment(stream_type& s, streambuf_type* new_sb) : m_s(s)
    {
        m_orig_sb = m_s.rdbuf(new_sb);
    }

    ~scoped_basic_streambuf_assignment()
    {
        m_s.rdbuf(m_orig_sb);
    }
};

typedef scoped_basic_streambuf_assignment<char> scoped_streambuf_assignment;
typedef scoped_basic_streambuf_assignment<wchar_t> scoped_wstreambuf_assignment;

#endif
